using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace IEeee
{
    class GDI32
    {
        [DllImport ("GDI32.dll")]
        public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
                                         int nWidth, int nHeight, IntPtr hdcSrc,
                                         int nXSrc, int nYSrc, int dwRop);
        [DllImport ("GDI32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth,
                                                         int nHeight);
        [DllImport ("GDI32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport ("GDI32.dll")]
        public static extern bool DeleteDC(IntPtr hdc);
        [DllImport ("GDI32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport ("GDI32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
        [DllImport ("GDI32.dll")]
        public static extern int SelectObject(IntPtr hdc, IntPtr hgdiobj);
    }

    class User32
    {
        [DllImport ("User32.dll")]
        public static extern IntPtr GetDesktopWindow ();
        [DllImport ("User32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);
        [DllImport ("User32.dll")]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    }


    class ScreenCapture
    {
        public static void CaptureScreen(string fileName, ImageFormat imageFormat)
        {
            CaptureScreen(User32.GetDesktopWindow(), fileName, imageFormat);
        }

        public static void CaptureScreen(IntPtr hWnd, string fileName, ImageFormat imageFormat)
        {
            IntPtr hdcSrc = IntPtr.Zero;
            IntPtr hdcDest = IntPtr.Zero;
            IntPtr hBitmap = IntPtr.Zero;
            try
            {
                hdcSrc = User32.GetWindowDC(hWnd);
                hdcDest = GDI32.CreateCompatibleDC(hdcSrc);

                int width = 0;
                int height = 0;

                User32.RECT rect;

                if (User32.GetWindowRect(hWnd, out rect))
                {
                    width = rect.Right - rect.Left;
                    height = rect.Bottom - rect.Top;
                }
                else
                {
                    width = GDI32.GetDeviceCaps(hdcSrc, 8);
                    height = GDI32.GetDeviceCaps(hdcSrc, 10);
                }

                hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
                
                GDI32.SelectObject(hdcDest, hBitmap);

                GDI32.BitBlt(hdcDest, 0, 0, width,
                                height,
                                hdcSrc, 0, 0, 0x00CC0020);

                using (Bitmap image = Image.FromHbitmap(hBitmap))
                {
                    image.Save(fileName, imageFormat);
                }

            }
            finally
            {
                if (hdcSrc != IntPtr.Zero)
                {
                    User32.ReleaseDC(hWnd, hdcSrc);
                }
                if (hdcDest != IntPtr.Zero)
                {
                    GDI32.DeleteDC(hdcDest);
                }
                if (hBitmap != IntPtr.Zero)
                {
                    GDI32.DeleteObject(hBitmap);
                }
            }
        }
    }
}